<template>
  <div class="content_card">
    <UHeader>
      <div slot="right">
        <el-button type="success" size="mini" @click="getGoodsClassifyForm={}; showDialog=true">添加</el-button>
      </div>
    </UHeader>
    <UDragTreeTable :data="treeData" />
    <el-dialog :visible.sync="showDialog" :append-to-body="true" :close-on-click-modal="false" width="400px" title="编辑菜单">
      <el-form ref="form" :model="getGoodsClassifyForm" style="padding: 0px 20px 0px 20px" label-width="80px" label-position="right" size="small">
        <el-form-item label="分类">
          <el-input v-model="getGoodsClassifyForm.name" placeholder="请输入分类名称"/>
        </el-form-item>
        <el-form-item label="排序权重" >
          <el-input v-model="getGoodsClassifyForm.sort" placeholder="请设置排序权重" type="number" min="0" />
        </el-form-item>
        <RolesSelect/>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" style="width: 98%" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import UDragTreeTable from '@/components/UDragTreeTable'
import UHeader from '@/components/UHeader'
import { getGoodsClassifys, editClassifys, delClassifys } from '@/api/system'
export default {
  components: {
    UDragTreeTable, UHeader
  },
  data() {
    return {
      showDialog: false,
      getGoodsClassifyForm: {},
      treeData: {
        lists: [],
        columns: [
          { type: 'selection', title: '分类', field: 'name', align: 'left', formatter: (item) => { return '<a>' + item.name + '</a>' } },
          { title: '图标', field: 'icon', align: 'center' },
          { title: '排序权重', field: 'sort', align: 'center' },
          { title: '创建时间', field: 'createTime', align: 'center' },
          { title: '创建人', field: 'createBy', align: 'center' },
          { title: '操作', type: 'action', width: 350, align: 'center', actions: [
            { text: '编辑', onclick: this.onEdit, formatter: (item) => { return '<i>编辑</i>' } },
            { text: '新增', onclick: this.addChild, formatter: (item) => { return '<i>新增</i>' } },
            { text: '删除', onclick: this.onDel, formatter: (item) => { return '<i>删除</i>' } }
          ]
          }
        ]
      }
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    loadData() {
      getGoodsClassifys().then(res => {
        this.treeData.lists = res.map(r => {
          r.open = false
          return r
        })
      })
    },
    save() {
      editClassifys(this.getGoodsClassifyForm).then(res => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.showDialog = false
        this.loadData()
      })
    },
    addChild(item) {
      if (item.parentId !== 0) {
        this.$message({
          message: '最多只能添加二级分类',
          type: 'error'
        })
        return
      }
      this.getGoodsClassifyForm = {}
      this.getGoodsClassifyForm.parentId = item.goodsClassifyId
      this.showDialog = true
    },
    doDel(goodsId) {
      delClassifys(goodsId).then(res => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.loadData()
      })
    },
    onDel: function(item) {
      if (item.children && item.children.length > 0) {
        this.$confirm('该分类下面还有子分类是否确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.doDel(item.goodsClassifyId)
        })
      } else {
        this.$confirm('是否确认删除该分类', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.doDel(item.goodsClassifyId)
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
